<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "edu_user".
 *
 * @property int $id
 * @property string $openid 用户openid
 * @property string $nickname 用户昵称
 * @property string $avatar_url 用户头像
 * @property int $sex 性别 1男 2女
 * @property string $city 城市
 * @property string $country 城市
 * @property string $province 省
 * @property string $phone 手机号
 * @property string $address 地址
 * @property int $status 状态
 * @property int $is_teach 0不是教师 1表示298教师 2表示29800教师
 * @property string $info 其他信息
 * @property int $create_time 创建时间
 * @property int $update_time 更新时间
 * @property int $mall_is 是否是商家
 * @property int $parent_id 父id
 * @property int $mall_enter_time 进入时间
 * @property int $mall_create_time  创建时间
 * @property string $mall_total_fee 用户可提取金额
 * @property string $mall_put_money 正在提取的金额
 * @property string $mall_rname 真实姓名
 * @property int $mall_superior 上级
 * @property int $mall_top 上上级
 * @property string $unionid 用户标识
 * @property string $score 积分
 * @property string $score_total 星级值
 * @property string $email 邮箱
 * @property string $recharge 商家or用户充值的金额
 * @property string $mall_appear_fee 用户提现的总额
 * @property string $is_red 是否开通了红包
 * @property string $mall_id 商家id
 */
class EduUser extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'edu_user';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['openid'], 'required'],
            [['sex', 'is_teach', 'create_time', 'update_time', 'parent_id', 'status', 'mall_is', 'mall_enter_time', 'mall_create_time', 'is_red', 'mall_superior', 'mall_top'], 'integer'],
            [['openid', 'unionid'], 'string', 'max' => 120],
            [['mall_is','mall_enter_time', 'mall_create_time', 'mall_id'], 'safe'],
            [['mall_total_fee', 'mall_put_money', 'score', 'score_total', 'recharge' , 'mall_appear_fee'], 'number'],
            [['nickname', 'mall_rname'], 'string', 'max' => 50],
            [['avatar_url'], 'string', 'max' => 500],
            [['city', 'province'], 'string', 'max' => 25],
            [['country', 'phone'], 'string', 'max' => 20],
            [['address'], 'string', 'max' => 100],
            [['info'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '会员号',
            'openid' => 'Openid',
            'nickname' => '昵称',
            'avatar_url' => '头像',
            'sex' => '性别',
            'city' => '城市',
            'country' => '国家',
            'province' => '省',
            'phone' => '电话',
            'address' => '地址',
            'is_teach' => '是否教师',
            'mall_is' => '是否是商家',
            'info' => '其他信息',
            'create_time' => '注册时间',
            'update_time' => '最后一次登录时间',
            'parent_id' => '上级(纹绣)',
            'status' => '状态',
            'mall_enter_time' => '进入时间',
            'mall_create_time' => '创建时间',
            'mall_total_fee' => '佣金(商城)',
            'mall_put_money' => '提取中',
            'mall_rname' => '真实姓名',
            'mall_superior' => '上级(商城)',
            'mall_top' => '上上级(商城)',
            'mall_id' => '商家id',
            'unionid' => 'unionid',
            'score'  => '积分',
            'score_total' => '星级值',
            'recharge' => '充值￥',
            'mall_appear_fee' => '已提现',
            'is_red' => '是否开通红包',
        ];
    }

    /**
     * @param $parent_id
     * @return integer
     * 统计下级
     */
    public static function parentCount($parent_id)
    {
         $count = (new \yii\db\Query())
            ->from('edu_user')
            ->where(['parent_id' => $parent_id])
            ->count();
         return $count;
    }

    /**
     * 定义is_teach
     * @return array
     */
    public static function attributeValue()
    {
        return [
            'is_teach' => [
                '0' => '普通用户',
                '1' => '普通教师',
                '2' => '特级教师',
            ],
            'sex' => [
                '0' => '未知',
                '1' => '男',
                '2' => '女',
            ],
            'status' => [
                '-1' => '禁用',
                '1' => '正常',
            ],
            'mall_is' => [
                '0' => '非商家',
                '1' => '商家',
            ],
            'is_red' => [
                '-1' => '否',
                '1' => '是',
            ],
        ];
    }

    /**
     * 获取用户状态map
     * @return string
     */
    public function getIsTeach()
    {
        switch ($this->is_teach)
        {
            case 0:
                $value = '普通用户';
                break;
            case 1:
                $value = '普通教师';
                break;
            case 2:
                $value = '特级教师';
                break;
            default:
                $value = '其他';
                break;
        }
        return $value;
    }

    /**
     * 性别
     * @return string
     */
    public function getSexValue()
    {
        switch ($this->sex)
        {
            case 0:
                $value = '未知';
                break;
            case 1:
                $value = '男';
                break;
            case 2:
                $value = '女';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    /**
     * 定义status
     * @return string
     */
    public function getStatusValue()
    {
        switch ($this->status)
        {
            case -1:
                $value = '禁止';
                break;
            case 1:
                $value = '正常';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    /**
     * 是否是商家
     * @return string
     */
    public function getMallIs()
    {
        switch ($this->mall_is)
        {
            case 0:
                $value = '非商家';
                break;
            case 1:
                $value = '商家';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    /**
     * 是否开通了红包
     * @return string
     */
    public function getIsRed()
    {
        switch ($this->is_red)
        {
            case -1:
                $value = '否';
                break;
            case 1:
                $value = '是';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    /**
     * 获取商家的id号
     * @return array
     */
    public static function getMall()
    {
        $sql = "SELECT uid, name FROM mall_business";
        $result =  Yii::$app->db->createCommand($sql)->queryAll();
        $emu = [];
        if ($result) {
            foreach ($result as $val) {
                $emu[$val['uid']] = $val['name'];
            }
            return $emu;
        } else {
            return $emu;
        }
    }
}
